import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:thinking/config/_.dart';
import 'package:thinking/entity/_.dart';
import 'package:thinking/utils/_.dart';
import 'package:thinking/widget/_.dart';
import 'package:thinking/service/_.dart';
class PersonalSub extends StatefulWidget{

  @override
  _PersonalSub createState() => _PersonalSub();


}

class _PersonalSub extends State<PersonalSub>{

  List<String> topItem = ['动态','关注','粉丝'];
  late Future<dynamic> _function;
  List<Tag> tagList = [];
  late TagClassPageEntity tagClassPageEntity  = TagClassPageEntity();
  EasyRefreshController _easyRefreshController = EasyRefreshController();


  Widget easyRefresh(Widget child) {
    return EasyRefresh(
      child: child,
      header: BezierCircleHeader(backgroundColor: ColorConfig.ThemeColor),
      footer: BezierBounceFooter(backgroundColor: ColorConfig.ThemeColor),
      enableControlFinishRefresh: false,
      enableControlFinishLoad: true,
      controller: _easyRefreshController,
      bottomBouncing: true,
      onLoad: () async {
        if (tagClassPageEntity.canLoad) {
          tagClassPageEntity.setPage = tagClassPageEntity.page + 1;
        }

        await getData();
        _easyRefreshController.finishLoad(success: true);
      },
    );
  }

  @override
  void initState() {
    super.initState();
    _function = future();

  }

  future() async{
    await getData();
  }

  Future<void> getData() async{
    List<Tag> res = await TopicService.getIndexAllTopic(params: {
      "topic_Index": 0,
      "topic_Page": tagClassPageEntity.page,
      "topic_Num": tagClassPageEntity.count,
      "user_Id": SPreferences().getInt('user_Id')
    });
    if (!tagClassPageEntity.canLoad) {
      BotToast.showText(text: '没有更多话题了哦~');
    } else {
      tagList = [...tagList, ...res];
    }
    if (res.length < tagClassPageEntity.count &&
        tagClassPageEntity.canLoad) {
      tagClassPageEntity.setPage = tagClassPageEntity.page + 1;
      tagClassPageEntity.setCanLoad = false;
    }
    tagClassPageEntity.setIsLoad = true;
    setState(() {});
  }



  @override
  void dispose() {
    _easyRefreshController.dispose();
    super.dispose();
  }

  Widget title(){
    return Row(
      children: [
        IconButton(onPressed: (){

          Modular.to.pop();

        }, icon: Icon(Icons.arrow_back),color: ColorConfig.WhiteBackColor,iconSize: 20,),
        Text(
         '我的订阅',
          style: TextStyle(
              color: ColorConfig.WhiteBackColor,
              fontSize: 16,
              fontWeight: FontWeight.bold
          ),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: ColorConfig.ThemeColor,
        automaticallyImplyLeading: false,
        elevation: 0,
        title: title(),

      ),
      body: FutureBuilder(
        future: _function,
        builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
          return snapshot.connectionState == ConnectionState.done?Container(
            child: easyRefresh(
                Empty(

                  child: ListView(
                    physics: BouncingScrollPhysics(),
                    shrinkWrap: true,
                    children: tagList.map((e) => TopicItemWidget(
                      isUpdate: true,
                      tagData: e,
                      type: 0,
                      key: ValueKey(e.id),
                    )).toList(),
                  ),
                  isShow: tagList.length == 0,
                )
            ),
          ):Loading();
        },

      ),
    );
  }

}